import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
import NProgress from '@/utils/nprogress.js'
import Home from '@/views/index/index.vue'
import { useCommonStore } from '@/stores'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home,
    },
    {
      path: '/tracking',
      name: 'tracking',
      meta: {
        title: 'Order Tracking',
      },
      component: () => import('@/views/tracking/index.vue'),
    },
    {
      path: '/about',
      name: 'about',
      meta: {
        title: 'About Yesgamers and Our Services',
      },
      component: () => import('@/views/about/index.vue'),
    },
    {
      path: '/faq',
      name: 'faq',
      meta: {
        title: 'FAQ - Frequently Asked Questions',
      },
      component: () => import('@/views/faq/index.vue'),
    },
    {
      path: '/bonus',
      name: 'bonus',
      meta: {
        title: 'Free Bonus D2R Items',
      },
      component: () => import('@/views/bonus/index.vue'),
    },
    {
      path: '/refer',
      name: 'refer',
      meta: {
        title: 'Refer A Friend',
      },
      component: () => import('@/views/refer/index.vue'),
    },
    {
      path: '/reviews',
      name: 'reviews',
      meta: {
        title: 'Reviews',
      },
      component: () => import('@/views/reviews/index.vue'),
    },
    {
      path: '/games',
      name: 'games',
      meta: {
        title: 'Games',
      },
      component: () => import('@/views/diablo/index.vue'),
      redirect: { name: 'products' },
      children: [
        {
          path: '',
          name: 'gamesCategory',
          meta: {
            title: 'Games Category',
          },
          component: () => import('@/views/diablo/gamesCategory.vue'),
        },
        {
          path: 'products',
          name: 'products',
          meta: {
            title: 'Buy Diablo 2 Items - (D2R) Fast, Reliable Delivery 24/7',
          },
          component: () => import('@/views/diablo/goodsList.vue'),
        },
        {
          path: 'raresOrCrafts',
          name: 'raresOrCrafts',
          meta: {
            title: 'Buy Diablo 2 Items - (D2R) Fast, Reliable Delivery 24/7',
          },
          component: () => import('@/views/diablo/raresOrCrafts.vue'),
        },
      ],
    },
    {
      path: '/login',
      name: 'login',
      meta: {
        title: 'Login to your Yesgamers Account | Buy Diablo 2 items @ Yesgamers',
      },
      component: () => import('@/views/login/index.vue'),
    },
    {
      path: '/contact',
      name: 'contact',
      meta: {
        title: 'Contact Us',
      },
      component: () => import('@/views/contact.vue'),
    },
    {
      path: '/cart',
      name: 'cart',
      meta: {
        title: 'Cart',
      },
      component: () => import('@/views/cart/index.vue'),
    },
    {
      path: '/credits',
      name: 'credits',
      meta: {
        title: 'Buy Diablo 2 Resurrected Items- Your D2 Items Delivered Instantly | Yesgamers',
      },
      component: () => import('@/views/credits.vue'),
    },
    {
      path: '/terms',
      name: 'terms',
      meta: {
        title: 'Terms &amp; Conditions',
      },
      component: () => import('@/views/terms.vue'),
    },
    {
      path: '/privacy',
      name: 'privacy',
      meta: {
        title: 'Privacy Policy',
      },
      component: () => import('@/views/privacy.vue'),
    },
    {
      path: '/refunds',
      name: 'refunds',
      meta: {
        title: 'Refund Policy',
      },
      component: () => import('@/views/refunds.vue'),
    },
    {
      path: '/cookies',
      name: 'cookies',
      meta: {
        title: 'Cookie Policy',
      },
      component: () => import('@/views/cookies.vue'),
    },
    {
      path: '/confirmPay',
      name: 'confirmPay',
      meta: {
        title: 'Confirm Payment',
      },
      component: () => import('@/views/pay/confirmPay.vue'),
    },
    //  重定向
    {
      path: '/:pathMatch(.*)*',
      redirect: 'home',
    },
  ],
  // 滚动行为
  // savedPosition 保存的位置，只有当 popstate 导航 (通过浏览器的 前进/后退 按钮触发) 时才可用。
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      // 滚动到之前的位置
      return savedPosition
    } else {
      // 新页面滚动到顶部
      return { top: 0 }
    }
  },
})
router.beforeEach((to, from, next) => {
  const { selectedArea } = useCommonStore()
  if (!selectedArea.key && to.name === 'gamesCategory') next('/')
  // 开启进度条
  NProgress.start()
  next()
})
router.afterEach((to, from) => {
  // 关闭进度条
  NProgress.done()
})
export default router
